package com.ohuang.wallpapermanager.util;

import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class AppUtil {

    private final Handler handler = new Handler(Looper.getMainLooper());
    private boolean isDebug=false;

    private AppUtil() {
    }

    private static final class InstanceHolder {
        static final AppUtil instance = new AppUtil();
    }

    public static AppUtil getInstance() {
        return InstanceHolder.instance;
    }

    private Application application;

    public void init(Application application,boolean isDebug) {
        this.application = application;
        this.isDebug=isDebug;
    }

    public boolean isDebug() {
        return isDebug;
    }

    public void toast(String s) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            Toast.makeText(application, s, Toast.LENGTH_SHORT).show();
        } else {
            post(() -> {
                Toast.makeText(application, s, Toast.LENGTH_SHORT).show();
            });
        }
    }

    public void toastLong(String s) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            Toast.makeText(application, s, Toast.LENGTH_LONG).show();
        } else {
            post(() -> {
                Toast.makeText(application, s, Toast.LENGTH_LONG).show();
            });
        }
    }

    public Context getContext() {
        return application;
    }

    public Application getApplication(){
        return application;
    }

    public String getString(int id) {
        return getContext().getString(id);
    }

    public Drawable getDrawable(int id) {
        return getContext().getDrawable(id);
    }


    public void post(Runnable runnable) {
        handler.post(runnable);
    }

    public void postDelayed(Runnable runnable, long delayed) {
        handler.postDelayed(runnable, delayed);
    }

    public void removeRunnable(Runnable runnable) {
        handler.removeCallbacks(runnable);
    }


}
